//
//  IOTCViewController.m
//  Tasks-Test3
//
//  Created by User-16 on 3/19/14.
//  Copyright (c) 2014 IOTrainingCamp. All rights reserved.
//

#import "IOTCAddTaskViewController.h"
#import "IOTCSetTaskStartAndEndDateViewController.h"

#import "IOTCTaskManager.h"
#import "IOTCTaskList.h"
#import "IOTCTask.h"

@interface IOTCAddTaskViewController ()

@property (retain, nonatomic) IBOutlet UITextView *taskDescriptionTextView;
@property (retain) id<IOTCTaskManager> tasks;

@end

@implementation IOTCAddTaskViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.taskDescriptionTextView.delegate = self;
    self.tasks = [IOTCTaskList sharedInstance];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)cancelTaskAddition:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)okAddATask:(id)sender {
    if([self.taskDescriptionTextView.text isEqualToString:@""])
    {
        UIAlertView* noDescriptionAllert = [[UIAlertView alloc] initWithTitle:@"No task description" message:@"You have to add a task description to add the task" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [noDescriptionAllert show];
        return;
    }
    IOTCTask* newTask = [[IOTCTask alloc] initWithTaskDescription:self.taskDescriptionTextView.text];
    [self.tasks addTask:newTask];
    
    
    IOTCSetTaskStartAndEndDateViewController *setTaskController = [[ IOTCSetTaskStartAndEndDateViewController alloc] initWithTask:newTask];
    [self.navigationController pushViewController:setTaskController animated:YES];
    

}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    
    return YES;
}


@end
